package com.wisdytech.linkdcs.equipment.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.linkdcs.equipment.dao.IEquipmentMqDao;
import com.wisdytech.linkdcs.equipment.model.EquipmentMQ;
import com.wisdytech.linkdcs.equipment.service.IEquipmentMqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * <p>
 * ClassName: EquipmentModelServiceImpl
 * </p>
 * <p>
 * Description: 设备类别表的业务实现类
 * </p>
 * <p>
 * Author: dailibo
 * </p>
 * <p>
 * Date: 2018年3月7日16:53:11
 * </p>
 */
@Service("equipmentMqService")
@Transactional
public class EquipmentMqServiceImpl implements IEquipmentMqService {
	
	private static Logger logger =LoggerFactory.getLogger(EquipmentMqServiceImpl.class);
	
	
	/**
	 * 设备类型队列dao
	 */
	@Resource
	private IEquipmentMqDao eqMqDao;
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	

	@Override
	public List<EquipmentMQ> getModelMqList(String modelId) {
		
		
		
		EntityWrapper<EquipmentMQ> ewEquipment = new EntityWrapper<>();
		//根据设备类型id查找所有的mq列表
		List<EquipmentMQ> listMq=
				eqMqDao.selectList(ewEquipment.eq("EQ_MODEL_ID", modelId));
		
		return listMq;
	}

	@Override
	public void insert(String modelId,List<EquipmentMQ> mqList) {	
		//首先删除之前的记录
		del(modelId);
		//批量添加
		for(EquipmentMQ mq : mqList){
			mq.setPkId(UUID.randomUUID().toString());
			mq.setModelId(modelId);
			eqMqDao.insert(mq);
		}
	}

	@Override
	public boolean del(String modelId) {
		EntityWrapper<EquipmentMQ> ewEquipment = new EntityWrapper<>();
		int result =eqMqDao.delete(ewEquipment.eq("EQ_MODEL_ID", modelId));
		return result>0;
	}

	@Override
	public void addTest(String key, String value) {
		logger.info("excute addTest key:{}",key);
		try {
			//判断是否存在
			String findSql ="select * from TT_EQ_TEST where TT_EQ_TEST.KEY='"+key+"'";
			List<Map<String, Object>>  result = jdbcTemplate.queryForList(findSql);
			if(null != result && result.size()>0) {
				String sql ="update TT_EQ_TEST set VALUE =?  where TT_EQ_TEST.KEY=?";
				//jdbcTemplate.update(sql, new Object[] {value,key}, new int [] {java.sql.Types.CLOB,java.sql.Types.VARCHAR});
				final LobHandler lobHandler=new DefaultLobHandler();
				jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

					@Override
					protected void setValues(PreparedStatement ps, LobCreator lobCreator)
							throws SQLException, DataAccessException {
						lobCreator.setClobAsString(ps, 1, value);
						ps.setString(2,key);

					}

				});
				//				jdbcTemplate.execute(sql);
			}else {
				String sql ="insert into TT_EQ_TEST (KEY,VALUE) values(?,?)";
//				jdbcTemplate.execute(sql,);
//				jdbcTemplate.update(sql, new Object[] {key,value}, new int [] {java.sql.Types.VARCHAR,java.sql.Types.CLOB});
				final LobHandler lobHandler=new DefaultLobHandler(); 
				jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

					@Override
					protected void setValues(PreparedStatement ps, LobCreator lobCreator)
							throws SQLException, DataAccessException {
						ps.setString(1,key);
						lobCreator.setClobAsString(ps, 2, value);
						
					}
					
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public String getTest(String key) {
		logger.info("excute getTest key:{}",key);
		try {
			//判断是否存在
			String findSql ="select * from TT_EQ_TEST where TT_EQ_TEST.KEY='"+key+"'";
			List<Map<String, Object>>  result = jdbcTemplate.queryForList(findSql);
			if(null != result && result.size()>0) {
				return (String) result.get(0).get("value");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
		
	}
	
	
	
	

}
